package android.slc.module.component.player.base;


import android.slc.module.component.player.constant.MediaPlayerStatus;
import android.slc.module.component.player.source.IMediaSource;

/**
 * 此类待完善
 * 比如播放状态需要加锁
 *
 * @author slc
 * @date 2022/05/18 上午 10:45
 */
public abstract class BaseMedia implements IMedia {
    private IMediaSource mediaSource;
    private MediaPlayerStatus mediaStatus = MediaPlayerStatus.UNKNOWN;

    private MediaPlayerEventListener mediaPlayerEventListener;

    private CacheHandler cacheHandler;

    @Override
    public IMediaSource getMediaSource() {
        return mediaSource;
    }

    @Override
    public void setMediaSource(IMediaSource mediaSource) {
        this.mediaSource = mediaSource;
    }

    @Override
    public boolean isPlaying() {
        return mediaStatus == MediaPlayerStatus.PLAYING;
    }

    @Override
    public MediaPlayerStatus getMediaStatus() {
        return mediaStatus;
    }

    @Override
    public void setMediaPlayerEventListener(MediaPlayerEventListener mediaPlayerEventListener) {
        this.mediaPlayerEventListener = mediaPlayerEventListener;
    }

    protected MediaPlayerEventListener getMediaPlayerEventListener() {
        return this.mediaPlayerEventListener;
    }

    @Override
    public CacheHandler getCacheAdapter() {
        return cacheHandler;
    }

    @Override
    public void setCacheAdapter(CacheHandler cacheHandler) {
        if (isPlaying()) {
            throw new RuntimeException("已经开始播放了，请在播放前设置录制适配器");
        }
        this.cacheHandler = cacheHandler;
        if (this.cacheHandler != null) {
            this.cacheHandler.setMedia(this);
        }
    }

    protected synchronized void onPrepare() {
        this.mediaStatus = MediaPlayerStatus.READY;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onPrepare();
        }
    }

    protected synchronized void onStart() {
        this.mediaStatus = MediaPlayerStatus.PLAYING;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onStart();
        }
        if (getCacheAdapter() != null) {
            getCacheAdapter().startCache();
        }
    }

    protected synchronized void onPause() {
        this.mediaStatus = MediaPlayerStatus.PAUSED;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onPause();
        }
        if (getCacheAdapter() != null) {
            getCacheAdapter().pauseCache();
        }
    }

    protected synchronized void onStop() {
        this.mediaStatus = MediaPlayerStatus.STOPPED;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onStop();
        }
        if (getCacheAdapter() != null) {
            getCacheAdapter().stopCache();
        }
    }

    protected synchronized void onError() {
        onError(0, "");
    }

    protected synchronized void onError(int errorCode, String errorMsg) {
        this.mediaStatus = MediaPlayerStatus.HALTED;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onError(errorCode, errorMsg);
        }
        if (getCacheAdapter() != null) {
            getCacheAdapter().stopCache();
        }
    }

    protected synchronized void onComplete() {
        this.mediaStatus = MediaPlayerStatus.STOPPED;
        if (getMediaPlayerEventListener() != null) {
            getMediaPlayerEventListener().onComplete();
        }
    }

    /**
     * 自动处理播放状态
     */
    @Override
    public int autoPlayStatus() {
        if (isPlaying()) {
            return stop();
        } else {
            return start();
        }
    }

}
